/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mrp.productionPlanning.managedbean;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import mrp.productionPlanning.entity.ForecastTableEntity;
import mrp.productionPlanning.session.ForecastTableSessionBean;
import org.primefaces.event.FlowEvent;

/**
 *
 * @author GKnight
 */
@ManagedBean
@ViewScoped
public class EditForecastTableManagedBean {

    private boolean skip;  
    private static Logger logger = Logger.getLogger(EditForecastTableManagedBean.class.getName()); 
    
    private List<ForecastTableEntity> forecastTableList;
    private int yearName;
    private String title;
    
    private double growthA;
    private double growthB;
    private double growthC;
    private double growthD;
    
    @EJB
    private ForecastTableSessionBean forecastTableManager = new ForecastTableSessionBean();
    
    /** Creates a new instance of EditForecastTableManagedBean */
    public EditForecastTableManagedBean() {
    }
    
    public void initView(PhaseEvent event) {

        if (getForecastTableList() == null) {
            setForecastTableList((ArrayList<ForecastTableEntity>) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("forecastTableList"));
        }
        yearName = forecastTableList.get(0).getYearFor();
        setTitle("Sales Forecast for Year "+yearName);
    }

    public void updateA(ActionEvent event){
        for(int i=0;i<forecastTableList.size();i++){
            forecastTableList.get(i).getForecast().setBaseAmountA((int)(forecastTableList.get(i).getHistoricalData().getBaseAmountA()*(1+growthA)));
            forecastTableList.get(i).getForecast().setGrowthrateA((int)(forecastTableList.get(i).getHistoricalData().getBaseAmountA()*growthA));
            forecastTableList.get(i).getForecast().setAmountA(forecastTableList.get(i).getForecast().getBaseAmountA()+forecastTableList.get(i).getForecast().getPromotionA());
        }
    }
    
    public void updateB(ActionEvent event){
        for(int i=0;i<forecastTableList.size();i++){
            forecastTableList.get(i).getForecast().setBaseAmountB((int)(forecastTableList.get(i).getHistoricalData().getBaseAmountB()*(1+growthB)));
            forecastTableList.get(i).getForecast().setGrowthrateB((int)(forecastTableList.get(i).getHistoricalData().getBaseAmountB()*growthB));
            forecastTableList.get(i).getForecast().setAmountB(forecastTableList.get(i).getForecast().getBaseAmountB()+forecastTableList.get(i).getForecast().getPromotionB());
        }
    }
     
    public void updateC(ActionEvent event){
        for(int i=0;i<forecastTableList.size();i++){
            forecastTableList.get(i).getForecast().setBaseAmountC((int)(forecastTableList.get(i).getHistoricalData().getBaseAmountC()*(1+growthC)));
            forecastTableList.get(i).getForecast().setGrowthrateC((int)(forecastTableList.get(i).getHistoricalData().getBaseAmountC()*growthC));
            forecastTableList.get(i).getForecast().setAmountC(forecastTableList.get(i).getForecast().getBaseAmountC()+forecastTableList.get(i).getForecast().getPromotionC());
        }
    }
    
    public void updateD(ActionEvent event){
        for(int i=0;i<forecastTableList.size();i++){
            forecastTableList.get(i).getForecast().setBaseAmountD((int)(forecastTableList.get(i).getHistoricalData().getBaseAmountD()*(1+growthD)));
            forecastTableList.get(i).getForecast().setGrowthrateD((int)(forecastTableList.get(i).getHistoricalData().getBaseAmountD()*growthD));
            forecastTableList.get(i).getForecast().setAmountD(forecastTableList.get(i).getForecast().getBaseAmountD()+forecastTableList.get(i).getForecast().getPromotionD());
        }
    }
    
    public void save(ActionEvent event){
        for (int i=0;i<forecastTableList.size();i++){
            forecastTableManager.saveForecastTable(forecastTableList.get(i));
        }
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Sales Forecast is Successfully Done.", ""));
    }

    public String onFlowProcess(FlowEvent event) {  
        logger.info("Current wizard step:" + event.getOldStep());  
        logger.info("Next step:" + event.getNewStep());  
          
        if(skip) {  
            skip = false;   //reset in case user goes back  
            return "confirm";  
        }  
        else {  
            return event.getNewStep();  
        }  
    }
    
    
    /**
     * @return the forecastTableList
     */
    public List<ForecastTableEntity> getForecastTableList() {
        return forecastTableList;
    }

    /**
     * @param forecastTableList the forecastTableList to set
     */
    public void setForecastTableList(List<ForecastTableEntity> forecastTableList) {
        this.forecastTableList = forecastTableList;
    }

    /**
     * @return the yearName
     */
    public int getYearName() {
        return yearName;
    }

    /**
     * @param yearName the yearName to set
     */
    public void setYearName(int yearName) {
        this.yearName = yearName;
    }

    /**
     * @return the growthA
     */
    public double getGrowthA() {
        return growthA;
    }

    /**
     * @param growthA the growthA to set
     */
    public void setGrowthA(double growthA) {
        this.growthA = growthA;
    }

    /**
     * @return the skip
     */
    public boolean isSkip() {
        return skip;
    }

    /**
     * @param skip the skip to set
     */
    public void setSkip(boolean skip) {
        this.skip = skip;
    }
    
    /**
     * @return the logger
     */
    public static Logger getLogger() {
        return logger;
    }

    /**
     * @param aLogger the logger to set
     */
    public static void setLogger(Logger aLogger) {
        logger = aLogger;
    }

    /**
     * @return the growthB
     */
    public double getGrowthB() {
        return growthB;
    }

    /**
     * @param growthB the growthB to set
     */
    public void setGrowthB(double growthB) {
        this.growthB = growthB;
    }

    /**
     * @return the title
     */
    public String getTitle() {
        return title;
    }

    /**
     * @param title the title to set
     */
    public void setTitle(String title) {
        this.title = title;
    }

    /**
     * @return the growthC
     */
    public double getGrowthC() {
        return growthC;
    }

    /**
     * @param growthC the growthC to set
     */
    public void setGrowthC(double growthC) {
        this.growthC = growthC;
    }

    /**
     * @return the growthD
     */
    public double getGrowthD() {
        return growthD;
    }

    /**
     * @param growthD the growthD to set
     */
    public void setGrowthD(double growthD) {
        this.growthD = growthD;
    }
    
}
